package com.midea.exam;

import java.util.concurrent.locks.LockSupport;

/**
 * @description：
 * @author: wangwl23
 * @create: 2021-12-29 17:13
 */
public class LockSupportToPrint {
    private static Thread t1;

    private static Thread t2;

    public static void main(String[] args) {
        char[] a1 = "1234567".toCharArray();
        char[] a2 = "ABCDEFG".toCharArray();

        t1 = new Thread(() -> {
            for(char c : a1){
                System.out.print(c);
                LockSupport.unpark(t2);//释放t2线程 设置锁标志位
                LockSupport.park();//阻塞当前线程
            }
        },"t1");

        t2 = new Thread(() -> {
            for(char c : a2){
                LockSupport.park();//阻塞当前线程，注意t1要先阻塞
                System.out.print(c);
                LockSupport.unpark(t1);//释放t1线程
            }
        },"t2");

        t1.start();
        t2.start();
    }
}
